package cn.otrue.patienthealthmanager.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.DrugPlanModel;
import cn.otrue.patienthealthmanager.utils.MyConst;

public class DrugListAdpater extends BaseAdapter {

	private List<DrugPlanModel> data;
	private LayoutInflater inflater;
	private Context context;

	public DrugListAdpater(Context context, List<DrugPlanModel> data) {
		super();
		this.data = data;
		this.context = context;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		arg1 = inflater.inflate(R.layout.item_drug_lv, null);
		DrugPlanModel drug = data.get(arg0);

		ImageView iv_drug_list_delete = (ImageView) arg1
				.findViewById(R.id.iv_drug_list_delete);
		TextView tv_drug_lv_name = (TextView) arg1
				.findViewById(R.id.tv_drug_lv_name);
		TextView tv_drug_lv_date = (TextView) arg1
				.findViewById(R.id.tv_drug_lv_date);
		TextView tv_drug_lv_breakfast = (TextView) arg1
				.findViewById(R.id.tv_drug_lv_breakfast);
		TextView tv_drug_lv_frequency = (TextView) arg1
				.findViewById(R.id.tv_drug_lv_frequency);
		TextView tv_drug_lv_dose = (TextView) arg1
				.findViewById(R.id.tv_drug_lv_dose);
		TextView tv_drug_lv_week = (TextView) arg1
				.findViewById(R.id.tv_drug_lv_week);
		TextView tv_drug_lv_note = (TextView) arg1
				.findViewById(R.id.tv_drug_lv_note);

		tv_drug_lv_name.setText(drug.getDrugName());
		if (!TextUtils.isEmpty(drug.getDrugbegintime())
				&& !TextUtils.isEmpty(drug.getDrugendtime())) {
			tv_drug_lv_date.setText(drug.getDrugbegintime().substring(5, 10)
					+ "~" + drug.getDrugendtime().substring(5, 10));
		}
		iv_drug_list_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				data.remove(arg0);
				notifyDataSetChanged();
			}
		});

		tv_drug_lv_breakfast.setText(drug.getBreakfast());
		tv_drug_lv_dose.setText(drug.getDrugdose());
		tv_drug_lv_frequency.setText(drug.getDrugfrequency());

		if (!TextUtils.isEmpty(drug.getNote())) {
			tv_drug_lv_note.setText(drug.getNote());
		}

		String weekStr = "";
		for (int i = 0; i < drug.getWeekList().size(); i++) {
			weekStr = weekStr
					+ " "
					+ MyConst.WEEK[Integer.parseInt(drug.getWeekList().get(i)) % 7];
		}
		tv_drug_lv_week.setText(weekStr);
		return arg1;
	}
}
